/*
3_FirstStage.do

Determine First Stages and make first stage output

"Human Capital and Climate Change", 
	by Noam Angrist, Kevin Winseck, Harry Patrinos, and Joshua Graff Zivin.
		
	
Inputs:
	Cleaned_ESS.dta
Outputs:
	Cleaned_ESS_withFirstStage.dta
	FirstStage.tex (Appendix Table A5)
	


*/


**************************** Set up data ****************************
* Set the variable to use to cluster standard errors
global clustervar "cntryXlaw"

use "Cleaned_ESS.dta", clear




levelsof cntry, local(cntryList)


******** Run first stages *******************************************
eststo clear 


* Make indicators for each CSL
levelsof cntryXlaw, local(reforms)
foreach reform in `reforms'{
	gen reform_`reform' = cntryXlaw=="`reform'"
	label var reform_`reform' "`reform'"
}

* Now replace the indicators of older reforms as 1 if a newer reform is also applicable
*	This is so that indicators are cumulative, so the first stage is whether the marginal effect of the current CSL is positive
levelsof cntry, local(countries)
foreach country in `countries'{
	levelsof minEdu if cntry=="`country'", local(laws)
	foreach law in `laws'{
		replace reform_`country'`law' = 1 if cntry=="`country'" & minEdu>`law' & !mi(minEdu)
	}
	
* Drop the earliest law that is in-sample to be the reference category
*	Any laws that don't apply at all should be dropped to colinearity anyway, so just need to drop the first applicable one	
	levelsof minEdu if cntry=="`country'" & in_sample==1, local(relevant_laws)
	local var_to_drop = word("`relevant_laws'", 1)
	rename reform_`country'`var_to_drop' ///
		omitted_reform_`country'`var_to_drop'
		*Rather than actually drop, we rename it to be "omitted" so that we may use "reform_*" to refer to all included reform indicators
}


* Choose first stages with positive first stages
* Putting cntry fixed effect first causes the reform indicator to be dropped in the case of colinearity. This is ideal because we want to be relative to the country FE, not to a reform that never applies
eststo: reg eduyrs_wins i.cntry_numeric  reform_* i.cntry_numeric#c.yr if in_sample==1, vce(cluster $clustervar)
estadd local TimeTrend "Linear by Country"
estadd local CountryFE "Yes"


* Write first stage output
* Appendix Table A5

file open tablefile using "FirstStage.tex", write replace
file write tablefile " \hline      Reform \& Year &Estimate & Positive & Positive+Significant \\ \hline\hline" _n
local n = e(N)
unab reforms: reform_*
foreach r in `reforms'{
	if _b[`r']>0{
		local b: display%9.3f _b[`r']
		local reformname = substr("`r'",8,.) /*write the 8th character through the end of reform name, like reform_AT8*/ 
		file write tablefile "`reformname'" 
		file write tablefile " & `b' & \checkmark & "
		*estimates restore est1
		local se: display%9.3f _se[`r']
		local t = _b[`r']/_se[`r']
		local p : display%9.3f 2*ttail(e(df_r),abs(`t'))
		
		di "this is the pval: `p'"
		if `p' < 0.05{
			file write tablefile "\checkmark "
		}
		file write tablefile "\\" _n
		qui su implementedYear if `r'==1
		local year: display%8.0g `r(min)'
		file write tablefile "`year' & (`se')~[`p'] & & \\ \hline" _n
	}
}
file write tablefile "Obserations & `n' & & \\ \hline"

file close tablefile


* Make indicators for being in positive+significant first stage, positive first stage, or all
gen beta = .
gen se = .
levelsof cntryXlaw, local(reforms)
foreach reform in `reforms'{
	cap replace beta = _b[reform_`reform'] if cntryXlaw=="`reform'" & in_sample==1
	cap replace se = _se[reform_`reform'] if cntryXlaw=="`reform'" & in_sample==1
}
* Rename indicators so they're kept but not used unless specifically requested
**** Construct sample markers for each (if there are no instruments for a country in the specification, it should not be included in the sample)
* Go through each country, if there is no reform, remove it from the sample

gen in_pooled = 0
gen in_pos = 0
gen in_pos_sig = 0
levelsof cntryXlaw, local(reforms)
foreach reform in `reforms'{
	local thiscntry = substr("`reform'",1,2)
	
	cap local thisbeta = _b[reform_`reform']
	cap local this_tstat = _b[reform_`reform'] / _se[reform_`reform']
	if _rc==0{
		* If there is a reform in this country (that isn't the omitted one), set all country people to in_pooled==1:
		replace in_pooled = 1 if cntry=="`thiscntry'"
		if (`thisbeta' >0 & `thisbeta'!=0 ){ /*0 is the value for an omitted variable*/
			* If the estimate is also positive, set in_pos=1 also:
			replace in_pos = 1 if cntry=="`thiscntry'"
			rename reform_`reform' reform_pos_only_`reform' /*indicates reforms that were positive*/
			if (`this_tstat' >=1.96 & !mi(`this_tstat')) {
				* If estimate is also significant, set in_pos_sig=1:
				replace in_pos_sig = 1 if cntry=="`thiscntry'"
				rename reform_pos_only_`reform' reform_pos_sig_`reform' /*indicates that were positive and significant*/
			}
		}
		else{
			rename reform_`reform' reform_only_`reform' /*indicates a reform that was not positive*/
		}
	}
}

save "Cleaned_ESS_withFirstStage.dta", replace





